package com.itheima.jdbctemplate;

import com.itheima.domain.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class JdbcTemplateDemo2 {
    public static void main(String[] args) {
        //获取容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取对象
        JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate",JdbcTemplate.class);
        //执行操作
        //jdbcTemplate.execute("insert into employee(emp_name,salary) values ('小王',20000.00)");

        //保存
       // jdbcTemplate.update("insert into employee(emp_name,salary) values(?,?)","eee",6666.0);
        //更新
        //jdbcTemplate.update("update employee set emp_name = ?,salary = ? where emp_id = ?","sss",10000.0,12);
        //删除
        //jdbcTemplate.update("delete from employee where emp_id = ?",14);
        //查询所有
        /*List<Employee> employees = jdbcTemplate.query("select emp_id empid,emp_name empname from employee",new BeanPropertyRowMapper<Employee>(Employee.class));
        for (Employee employee : employees) {
            System.out.println(employee);
        }*/
        //查询一个
        List<Employee> employees = jdbcTemplate.query("select emp_id empid,emp_name empname,salary from employee where emp_id = ?",new BeanPropertyRowMapper<Employee>(Employee.class),11);
        System.out.println(employees.isEmpty()?"没有内容":employees.get(0));
        //查询返回一行一个列（使用聚合函数，但不加group by子句）
        Long count = jdbcTemplate.queryForObject("select count(*) from employee",Long.class);

        System.out.println(count);
    }
}
